package iotwisp.school.api.controller.pc_admin.user;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.ValidationUtils;
import iotwisp.school.user.command.user.CreateUserSCmd;
import iotwisp.school.user.command.user.ModifyUserSCmd;
import iotwisp.school.user.command.user.UserLoginSCmd;
import iotwisp.school.user.command.user.UserRegisterSCmd;
import iotwisp.school.user.entity.LoginToken;
import iotwisp.school.user.entity.User;
import iotwisp.school.user.qo.UserQO;
import iotwisp.school.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/org/user")
public class UserController extends RestBaseController {

    @Autowired
    private UserService userService;

    @SuppressWarnings("unchecked")
	@PostMapping("/query")
	public ResponseEntity<JSONResultDTO> queryUser(@RequestBody UserQO qo) throws BusinessException {

		ValidationUtils.validate(qo);

		if (qo.getFetchRoles()) {
			qo.setBatchResult(true);
		}
		switch (qo.getResultType()) {
		case UserQO.RESULT_TYPE_COUNT:
			Integer count = userService.queryCount(qo);
			return ResponseEntity.ok(createSuccessResult(count));
		case UserQO.RESULT_TYPE_UNIQUE:
			User user = userService.queryUnique(qo);
			return ResponseEntity.ok(createSuccessResult(user));
		case UserQO.RESULT_TYPE_LIST:
			List<User> users = userService.queryList(qo);
			return ResponseEntity.ok(createSuccessResult(users));
		case UserQO.RESULT_TYPE_PAGINATION:
			Pagination pagination = userService.queryPagination(qo);
			return ResponseEntity.ok(createSuccessResult(pagination));
		default:
			return ResponseEntity.badRequest()
					.body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
		}
	}

    @PostMapping("/register")
    public ResponseEntity<JSONResultDTO> register(@RequestBody UserRegisterSCmd cmd) throws BusinessException {

        User user = userService.register(cmd);
        return ResponseEntity.ok(createSuccessResult(user));
    }

    @PostMapping("/login")
    public ResponseEntity<JSONResultDTO> userLogin(@RequestBody UserLoginSCmd cmd) throws BusinessException {

        LoginToken loginToken = userService.userLogin(cmd);
        return ResponseEntity.ok(createSuccessResult(loginToken));
    }

    @PostMapping("/create")
    public ResponseEntity<JSONResultDTO> create(@RequestBody CreateUserSCmd cmd) throws BusinessException {

        ValidationUtils.validate(cmd);

        User user = userService.createUser(cmd);
        return ResponseEntity.ok(createSuccessResult(user));
    }

    @PutMapping("/modify")
    public ResponseEntity<JSONResultDTO> modifyUser(@RequestBody ModifyUserSCmd cmd) throws BusinessException {
        ValidationUtils.validate(cmd);

        User user = userService.modifyUser(cmd);

        return ResponseEntity.ok(createSuccessResult(user));
    }

}
